from flask import Flask
from flask import current_app, request, make_response, jsonify

app = Flask(__name__)

app.config.from_pyfile("config.cfg")


@app.route("/index")
def index():
    resp = make_response("index page 2")
    resp.headers["city"] = "深圳".encode("utf-8")
    return resp


@app.route('/index/<int:test123>')
def index1(test123):
    return "hello flask%s" % test123


@app.route("/form", methods=["POST"])
def test():
    name = request.form.get("name")
    age = request.form.get("age")
    print("request.data = %s" % request.data.decode("utf-8"))
    return "name=%s,age=%s" % (name, age)


@app.route("/upload", methods=["POST"])
def upload():
    f = request.files.get("pic")
    if f is None:
        return "未上传文件"
    with open("./2.jpg", 'wb') as file:
        file.write(f.read())
    return "上传成功"


@app.route("/json_test")
def json_test():
    data = {
        "name": "张三",
        "age": 22
    }
    return jsonify(data)


if __name__ == '__main__':
    app.run(host=app.config.get("HOST"))
